#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define lld long long // long long 的 printf 占位符是 lld
#define ENDL '\n'     // 将 endl 替换为 \n 取消缓冲区

using std::cin;
using std::cout;
using std::vector;

void solve() {
  std::string s;
  cin >> s;
  lld cnt = 0;
  auto opt1 = [&]() {
    if (s[s.size() - 1] != '0') {
      cnt += s[s.size() - 1] - '0';
      s[s.size() - 1] = '0';
    }
  };
  auto opt2 = [&]() {
    if (s == "0") {
      s.pop_back();
      return;
    } else if (s != "0")
      s.pop_back(), cnt += 1;
  };

  if (s.size() == 1) {
    cout << s[0] - '0' << ENDL;
  } else {
    opt1();
    s.erase(std::remove(s.begin(), s.end(), '0'), s.end());
    cnt++;
    while (!s.empty()) {
      opt1();
      opt2();
    }
    cout << cnt << ENDL;
  }
}
int main() {
  int T = 1;
  cin >> T;
  while (T--)
    solve();
}